//
//  MSMineTableViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/1.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSMineTableViewController.h"

@interface MSMineTableViewController ()

@end

@implementation MSMineTableViewController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [(NSArray *)self.dataArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"headCell";
    static NSString *cellId2 = @"mineCell";
    
    MSMineHeadCell *headCell = [tableView dequeueReusableCellWithIdentifier:cellId];
    MSMineCell *commanCell = [tableView dequeueReusableCellWithIdentifier:cellId2];
    
    if (indexPath.section == 0) {
        if (!headCell) {
            headCell = [[MSMineHeadCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
        }
        headCell.userModel = self.dataArray[indexPath.section][indexPath.row];
        return headCell;
    }
    else{
        if (!commanCell) {
            commanCell = [[MSMineCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId2];
            commanCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        commanCell.data = self.dataArray[indexPath.section][indexPath.row];
        if (indexPath.row == [self.dataArray[indexPath.section] count]-1) {
            commanCell.horizonLineView.hidden = YES;
        }
        else{
            commanCell.horizonLineView.hidden = NO;
        }
        return commanCell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 100.0f;
    }else {
        return 50.0f;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 16.0f;
    }else {
        return 8.0f;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENFRAME.size.width, 20)];
    bgView.backgroundColor = kDefaultViewBackgroundColor;
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [bgView addSubview:horizonLineView];
    return bgView;
}

@end


@interface MSMineHeadCell ()
@property (nonatomic, strong) DOImageView *icon;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *positionLabel;

@end

@implementation MSMineHeadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    self.icon = [[DOImageView alloc] initWithFrame:CGRectMake(16, 20, 60, 60)];
//    self.icon.imgURLStr = @"logo60";
    self.icon.layer.cornerRadius = 30;
    self.icon.clipsToBounds = YES;
    [self.contentView addSubview:self.icon];
    
    self.nameLabel = [self creatLabel:CGRectMake(86, 28, SCREEN_WIDTH-90, 20)];
    self.positionLabel = [self creatLabel:CGRectMake(86, 52, SCREEN_WIDTH-90, 20)];
    
    UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-30, 42, 8.5, 16.5)];
    arrow.image = [UIImage imageNamed:@"arrow2"];
    [self.contentView addSubview:arrow];
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kLightBlackColor;
    [self addSubview:label];
    return label;
}

- (void)setUserModel:(MSUserInfoModel *)userModel{
    _userModel = userModel;
    if (userModel.headIcon.length) {
        self.icon.imgURLStr = userModel.headIcon;
    }
    else{
        self.icon.defaultImg = @"logo60";
    }
    NSMutableAttributedString *nameAttStr = [[NSMutableAttributedString alloc] initWithString:userModel.name attributes:nil];
    
//    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
//    textAttachment.bounds = CGRectMake(4, -1, 15, 12.5);
//    textAttachment.image = [UIImage imageNamed:@"diamond"];
//    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
//    [nameAttStr insertAttributedString:attachmentString atIndex:userModel.name.length];
    
//    self.nameLabel.text = userModel.name;
    self.nameLabel.attributedText = nameAttStr;
    self.positionLabel.text = [NSString stringWithFormat:@"%@   %@",userModel.company,userModel.position];
}

@end

@interface MSMineCell ()

@end

@implementation MSMineCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        self.textLabel.font = HeitiFont(15);
        self.detailTextLabel.font = HeitiFont(14);
        self.detailTextLabel.textColor = kLightGrayColor;
    }
    return self;
}

- (void)initView {
    _horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(16, 49.4, SCREEN_WIDTH-32, 0.6)];
    _horizonLineView.backgroundColor = kNormalGray;
    [self.contentView addSubview:_horizonLineView];
}

- (void)setData:(DOKeyValue *)data {
    _data = data;
    self.imageView.image = [UIImage imageNamed:data.placeHolder];
    self.textLabel.text = data.key;
    self.detailTextLabel.text = data.value;

    if ([data.key isEqualToString:@"江湖币"]) {
        NSMutableAttributedString *getAttStr =  [[NSMutableAttributedString alloc] initWithString:data.value];
        
        NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        textAttachment.bounds = CGRectMake(0, -2.5, 15, 15);
        textAttachment.image = [UIImage imageNamed:@"coin"];
        NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
        [getAttStr insertAttributedString:attachmentString atIndex:0];
        self.detailTextLabel.attributedText = getAttStr;
    }
}

@end
